<?
#	This file is part of SmartWebIF.
#	
#	SmartWebIF is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	SmartWebIF is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with SmartWebIF.  If not, see <http://www.gnu.org/licenses/>.

define ("SMARTWEBIFVER","0.3.1b");

include 'functions.php';
$config = include 'config.php';

if ($_POST["auth"] <> '')
	{
	setcookie("auth",md5($_POST["auth"]));
	setcookie("time",time()+1800);
	}

$config["tab"] = 4;
if ($config["stream"] == '') $config["stream"] = 'embedded';

if (isset($_GET["password"]) and $_GET["password"] <> '') $config["password"] = md5($_GET["password"]);
if (isset($_GET["password"]) and $_GET["password"] == '') $config["password"] = $_GET["password"];
if (isset($_GET["hwid"]) and $_GET["hwid"] == '') $config["hwid"] = $_GET["hwid"];

if ($_GET["sdvb_path"] <> '') $config["sdvb_path"] = $_GET["sdvb_path"];
if ($_GET["data_path"] <> '') $config["data_path"] = $_GET["data_path"];
if ($_GET["vlc_path"] <> '') $config["vlc_path"] = $_GET["vlc_path"];
if ($_GET["stream1"] <> '') $config["stream1"] = $_GET["stream1"];
if ($_GET["stream1_url"] <> '') $config["stream1_url"] = $_GET["stream1_url"];
if ($_GET["stream1"] <> '') $config["stream2"] = $_GET["stream2"];
if ($_GET["stream2_url"] <> '') $config["stream2_url"] = $_GET["stream2_url"];
if ($_GET["norestart"] <> '') $config["norestart"] = $_GET["norestart"];
if ($_GET["hwid"] <> '') $config["hwid"] = $_GET["hwid"];
if ($_GET["date_format"] <> '') $config["date_format"] = $_GET["date_format"];
if ($_GET["tab"] <> '') $config["tab"] = $_GET["tab"];
if ($_GET["sortname"] <> '') $config["sortname"] = $_GET["sortname"];
if ($_GET["sortorder"] <> '') $config["sortorder"] = $_GET["sortorder"];
if ($_GET["selr"] <> '')$config["selr"] = $_GET["selr"];
if ($_GET["satid"] <> '') $config["satid"] = $_GET["satid"];
if ($_GET["sid"] <> '') $config["sid"] = $_GET["sid"];
if ($_GET["tid"] <> '') $config["tid"] = $_GET["tid"];
if ($_GET["nid"] <> '') $config["nid"] = $_GET["nid"];
if ($_GET["fta"] <> '') $config["fta"] = $_GET["fta"];
if ($_GET["crypted"] <> '') $config["crypted"] = $_GET["crypted"];
if ($_GET["tv"] <> '') $config["tv"] = $_GET["tv"];
if ($_GET["radio"] <> '') $config["radio"] = $_GET["radio"];
if ($_GET["data"] <> '') $config["data"] = $_GET["data"];
if ($_GET["stream"] <> '') $config["stream"] = $_GET["stream"];

$record = check_record($config);

$sdvb = get_sdvb_info($config);
if ($sdvb["ver"] == '') $sdvb["ver"] = 'unknown';
if (substr($sdvb["build"],0,10) == '01.01.1970') $sdvb["build"] = 'unknown';
if ($sdvb["vlcver"] == '') $sdvb["vlcver"] = 'unknown';
if ($sdvb["satdevname"] == '') $sdvb["satdevname"] = 'Unknown device';
if ($sdvb["tab"] <> '') $config["tab"] = $sdvb["tab"];
$config["rec_path"] = $sdvb["rec_path"];
$config["udp_port"] = $sdvb["udp_port"];
if ($config["satid"] == '') $config["satid"] = $sdvb["satid"];
if ($config["sid"] == '') $config["sid"] = $sdvb["sid"];
if ($config["tid"] == '')$config["tid"] = $sdvb["tid"];

$control = $_GET["control"];
if ($control == 'select_sdvb_channel')
	{
	$config["satid"] = $sdvb["satid"];
	$config["sid"] = $sdvb["sid"];
	$config["tid"] = $sdvb["tid"];
	$service = get_service_info($config,0);
	$config["nid"] = $service["nid"];
	}
if ($control == 'start_sdvb') $config["sdvb"] = start_sdvb($config);
if ($control == 'stop_sdvb') $config["sdvb"] = stop_sdvb($config);
if (($control == 'start_vlc') and (!$sdvb["vlc_path_err"])) $config["vlc"] = start_vlc($config);
if ($control == 'stop_vlc') $config["vlc"] = stop_vlc($config);
if ($control == 'restart_device') restart_device($config);
if ($control == 'start_record') start_record($config,$_GET["rectime"]);
if ($control == 'stop_record') stop_record($config,$record);
if ($control == 'schedule')
	{
	schedule($config,$_GET["event"],$_GET["datetime"],$_GET["enddatetime"],$_GET["schpass"]);
	$config["tab"] = '3';
	}
if ($control == 'delete_task')
	{
	delete_task($_GET["task"]);
	$config["tab"] = '2';
	}
if (($control == 'play') and (!$sdvb["vlc_path_err"]))
	{
	stop_vlc($config);
	$config["vlc"] = start_vlc($config,$_GET["video"]);
	}
if ($control == 'delete_recording')
	{
	delete_recording($config,$_GET["file"]);
	$config["tab"] = '2';
	}

$sdvb_proc = explode("\"",shell_exec('tasklist /NH /FO csv /FI "IMAGENAME eq SmartDVB.exe"'));
$sdvb_pid = str_replace('"', "", $sdvb_proc["3"]);
$vlc_proc = explode("\"",shell_exec('tasklist /NH /FO csv /FI "IMAGENAME eq vlc.exe'));
$vlc_pid = str_replace('"', "", $vlc_proc["3"]);

file_put_contents('config.php', '<? return ' . var_export($config, true) . ';?>');

$record = check_record($config);

if (!isset($_GET["tv"]) and ($sdvb_pid <> '') and ($vlc_pid <> ''))
	{
	start_sdvb($config);
	if ($config["vlc"] and !$config["norestart"] and !$sdvb["vlc_path_err"] and !$_GET["video"])
		{
		stop_vlc($config);
		start_vlc($config);
		}
	}

echo '
	<!doctype html>
	 
	<html lang="en">

	<head>
	  <meta charset="utf-8">
	  <title>SmartWebIF</title>
	  <link rel="stylesheet" href="css/jquery-ui.css">
	  <link rel="stylesheet" href="css/ui.jqgrid.css">
	  <link rel="stylesheet" href="css/style.css">
	  <script src="scripts/jquery-1.10.2.min.js"></script>
	  <script src="scripts/jquery-ui.js"></script>
	  <script src="scripts/grid.locale-en.js" type="text/javascript"></script>
	  <script src="scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
	  <script>
		jQuery(document).ready(function(){
		jQuery("#channels").jqGrid({
			url: "channels.php?satid='.$config["satid"].'",
			datatype: "json",
			colNames:["SAT_ID","SID","TID","NID","Channel","Provider","Freq."],
			colModel:[
				{name:"sat_id",index:"sat_id", hidden:true},
				{name:"sid",index:"sid", hidden:true},
				{name:"tid",index:"tid", hidden:true},
				{name:"nid",index:"nid", hidden:true},
				{name:"servicename",index:"servicename"},
				{name:"serviceprovidername",index:"serviceprovidername"},
				{name:"frequency",index:"frequency", width:35},
				],
			height: $("#container").height(),
			rowNum: 3000,
			sortname: "'.$config["sortname"].'",
			sortorder: "'.$config["sortorder"].'",
			scrollOffset: 0,
			autowidth: true,
			loadonce: true,
			ignoreCase: true,
			loadComplete: function () { 
				$(this).jqGrid("setSelection","'.$config["selr"].'",false);
				},
';
if ($record["sid"] == '')
echo '
			onSelectRow: function(id) {
				var sat_id = $(this).jqGrid("getCell", id, "sat_id");	
				var sid = $(this).jqGrid("getCell", id, "sid");	
				var tid = $(this).jqGrid("getCell", id, "tid");	
				var nid = $(this).jqGrid("getCell", id, "nid");
				var sortname = $(this).jqGrid("getGridParam", "sortname");
				var sortorder = $(this).jqGrid("getGridParam", "sortorder");
				var selr = $(this).jqGrid("getGridParam","selrow");
				var searchfilters = $(this).getGridParam("postData").filters;
				window.location = "index.php?satid=" + sat_id + "&sid=" + sid + "&tid=" + tid + "&nid=" + nid + "&sortname=" + sortname + "&sortorder=" + sortorder + "&selr=" + selr;
				},
';
echo '
			});
		$("#channels").jqGrid("filterToolbar",{stringResult: true, searchOnEnter: false, defaultSearch: "cn"});
		});
		$(function() {$(".b-start").button({icons:{primary:"ui-icon-play"}});});
		$(function() {$(".b-stop").button({icons:{primary:"ui-icon-stop"}});});
		$(function() {$(".b-refresh").button({icons:{primary:"ui-icon-refresh"}});});
		$(function() {$(".b-load").button({icons:{primary:"ui-icon-extlink"}});});
		$(function() {$(".b-switch").button({icons:{primary:"ui-icon-transferthick-e-w"}});});
		$(function() {$(".b-record").button({icons:{primary:"ui-icon-bullet"}});});
		$(function() {
		$( "#password-message" ).dialog({
			modal: true,
			resizable: false,
			dialogClass: "no-close",
			closeOnEscape: false,
			buttons: {
				Ok: function() {
					$(this).find("form").submit();
					}
				}
			});
		});
		$(function() {
			$( "#tabs" ).tabs({
				beforeLoad: function( event, ui ) {
				ui.jqXHR.error(function() {
					ui.panel.html("Tab not found." );
					});
				}
			});
		});
		$(function() {
			$( "#tabs" ).tabs({active: '.$config["tab"].'});
			});
	</script>
	</head>

	<body>
';
if ($config["password"] <> '')
	{
	if ((md5($_POST["auth"]) <> $config["password"]) and ($_COOKIE["time"] < time()))
		{
		show_password_dialog();
		echo '</body></html>';
		die();
		}
	if ((md5($_POST["auth"]) <> $config["password"]) and ($_COOKIE["auth"] <> $config["password"]))
		{
		show_password_dialog();
		echo '</body></html>';
		die();
		}
	}
echo '
  <div id="main">

  <div id="header">
    <table width="100%" cellspacing="0" cellpadding="0"><tr>
      <td><img src="images/logo.png" id="logo"></td>
      <td align="right"><div id="info">
		SmartWebIF version: <b>'.SMARTWEBIFVER.'</b><br>
		SmartDVB version: <b>'.$sdvb["ver"].'</b><br>
		SmartDVB build: <b>'.$sdvb["build"].'</b><br>
		VLC version: <b>'.$sdvb["vlcver"].'</b>
     </div></td>
    </tr></table>
  </div>

  <div id="tabs">
    <ul>
	<li><a href="#tabs-2">About</a></li>
	<li><a href="settings.php">Settings</a></li>
	<li><a href="dvr.php">DVR</a></li>
	<li><a href="epg.php">EPG</a></li>
    <li><a href="#tabs-1">'.$sdvb["satdevname"].'</a></li>
    </ul>

  <div id="tabs-1">
	';
	
# Left column

echo '<table><tr><td id="left_column">';

if (!$sdvb["data_path_err"]) show_channel_list($config);

echo '</td>';

# Right column

echo '<td id="right_column">';

show_status($config,$sdvb_pid,$vlc_pid);

if (!$sdvb["data_path_err"]) $service = get_service_info($config,1);
if ($service["servicename"]) show_video($config,$service,$record,$sdvb_pid,$vlc_pid,$_GET["video"]);
echo '</td></tr></table>';

echo '
  </div>

  <div id="tabs-2">
    <div class="highlight">SmartWebIF '.SMARTWEBIFVER.'</div>
	Copyright &copy; 2013 Matei Dobrescu<br><br>
	Please direct all questions to <a href="mailto:smartwebif-mail@yahoo.com">smartwebif-mail@yahoo.com</a><br><br>
	SmartWebIF is open source software, released under the terms of the <a href="http://www.gnu.org/licenses/gpl.html">GNU GPL</a> license.<br><br>
	Uses or incorporates code from the following open source projects:
	<ul>
	<li><a target="_blank" href="http://php.net">PHP</a> - copyright © 2001-2013 The PHP Group</li>
	<li><a target="_blank" href="http://code.google.com/p/mongoose/">Mongoose</a> - copyright © 2013 Cesanta Software Ltd</li>
	<li><a target="_blank" href="http://www.openssl.org">OpenSSL</a> - copyright © 1998-2011 The OpenSSL Project</li>
	<li><a target="_blank" href="http://jquery.org">jQuery</a> - copyright © 2005, 2013 jQuery Foundation, Inc.</li>
	<li><a target="_blank" href="http://jqueryui.com">jQueryUI</a> - copyright © 2013 jQuery Foundation and other contributors</li>
	<li><a target="_blank" href="http://www.trirand.com">jqGrid</a> - copyright © 2013 Tony Tomov</li>
	<li><a target="_blank" href="http://flv-player.net">FLV Player</a></li>
	</ul>
  </div>

  </div>

  <div id="footer">
    SmartWebIF '.SMARTWEBIFVER.'
  </div>
	 
</div>

<br>
</body>

</html>
';
?>